<?php
class LichdatsanController extends Controller{
	/**
	 * Declares class-based actions.
	 */
	public $layout='//layouts/column3';
	
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		$ngay = DateUtils::getStringThisMonday();
		$loai = 'tuanhientai';
		if(isset($_REQUEST['ngay'])){
			$ngay = $_REQUEST['ngay'];
		}
		if(isset($_REQUEST['loai'])){
			$loai = $_REQUEST['loai'];
		}
		
		$list  = array();
		$san = Sanbong::model()->findAll();
		$i=0;
		foreach($san as $item){
			$list[$i] = $this->loadsaninfo($item,$ngay);
			//print_r($list[$i]['lich']);
			//echo "<br/><br/>";
			$i++;
		}
		//die();
		$this->render('index', array('danhsachlich'=>$list, 'ngay'=>$ngay));
	}
	
	
	private function loadsaninfo($san,$ngay){
		$criteria = new CDbCriteria();
		 $criteria->addCondition("ngaydat>='".$ngay."'");
		 $tuantoi= new DateTime($ngay);
		 $tuantoi->modify("+7 days");
		 $criteria->addCondition("ngaydat<'".DateUtils::createDate($tuantoi)."'");
		 $criteria->compare("idsan",$san->id,true);
		 $criteria->order = 'ngaydat';
		 
		 $ds = Lichdatsan::model()->findAll($criteria);
		 $count = count($ds);
		 $list=array();$j=0;
		 $step =  new DateTime($ngay);

		 for($i=0; $j < 7;){
		 	$item = array('','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','');
		 	if($i >= $count){		 		
			 	$list[$j]=$item;
			 	$j++;
			 	$step->modify("+1 days");
			 	continue;
		 	}
		 	if( date_format($step, "Y-m-d") == strstr($ds[$i]->ngaydat, ' ', true)){
		 		for($k=0; $k < 34; $k++){
		 			$item[$k] = $ds[$i]->attributes['g'.$k];
		 		}
		 		$i++;
		 	}
		 	$list[$j]=$item;
		 	$j++;
		 	$step->modify("+1 days");
		 }
		 $data['san'] = $san;
		 $data['lich'] = $list;
		 return $data;
	}
	
	
	public function lichdatsan(){
		$ngay = DateUtils::getThisMonday();
		$loai = 'tuanhientai';
		if(isset($_REQUEST['ngay'])){
			$ngay = $_REQUEST['ngay'];
		}
		if(isset($_REQUEST['loai'])){
			$loai = $_REQUEST['loai'];
		}
		
		$criteria = new CDbCriteria();
		
		 $criteria->addCondition("ngaydat>=".$ngay);
		 $tuantoi= new DateTime($ngay);
		 $tuantoi->modify("+7 days");
		 $criteria->addCondition("ngaydat<".DateUtils::createDate($tuantoi));
		 $criteria->order = 'ngaydat';
		 
		 $ds = Datsan::model()->findAll($criteria);
		 print_r($ds);die();
	}
	
}

?>